package com.yl.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Permission;
import com.yl.auth.entity.Role;
import com.yl.auth.model.entity.Gangwei;
import com.yl.auth.model.entity.GangweiQuanxian;
import com.yl.auth.model.entity.Quanxian;
import com.yl.auth.model.entity.Yuangong;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 岗位
 * @author
 * @since 2023-06-15
 */
@Api(tags = "人事行政-岗位管理")
@RestController
@RequestMapping("/admin/auth/gangwei")
public class GangweiController {

    @Autowired
    private GangweiService gangweiService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private YuangongService yuangongService;
    @Resource
    private RoleService roleService;

    /**
     * 分页模糊查询
     * @param page
     * @param limit
     * @param
     * @return
     */
    @ApiOperation("条件分页查询")
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit) {
        Page<Role> page1 = new Page<>(page, limit);
        IPage<Role> page2 = roleService.getAll(page1);
        return Result.ok(page2);
    }

    /**
     * 修改信息
     * @param gangwei
     * @return
     */
    @ApiOperation("修改信息")
    @PutMapping("/update")
    public Result update(@RequestBody Role gangwei,@RequestParam(value = "quanxianList")List<Integer> quanxianList){
        roleService.updateById(gangwei);
        gangweiService.updateQuanXian(gangwei.getId(),quanxianList);
        return Result.ok();
    }
    @ApiOperation("根据id查询权限信息")
    @GetMapping("/getQuanXianById/{id}")
    public Result getQuanXianById(@PathVariable Long id) {
        List<Integer> list = gangweiService.getQuanXianById(id);
        return Result.ok(list);
    }


    @ApiOperation("全查权限")
    @GetMapping("getAll")
    public Result list(){
        List<Permission> list= permissionService.getAll();
        return Result.ok(list);
    }
    /**
     * 添加信息
     * @param
     * @return
     */
    @ApiOperation("添加信息")
    @PostMapping("/save")
    public Result save(@RequestBody Role role, @RequestParam(value = "quanxianList")List<Integer> quanxianList){
        role.setType(3);
        role.setCreateTime(LocalDateTime.now());
        roleService.save(role);
        gangweiService.addQuanXian(role.getId(),quanxianList);
        return Result.ok();
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @ApiOperation("删除信息")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Integer id){
        //删除中间表
        roleService.removeBiao(id);
        roleService.removeByDelId(id);
        return Result.ok();

    }

    /**
     * 查询全部信息
     * @return
     */
    @ApiOperation("查询全部信息")
    @GetMapping("/gangweiList")
    public Result gangweiList(){
        List<Role> gangweiList = roleService.getAllByType();
        return Result.ok(gangweiList);
    }

    /**
     * 根据id查询信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询信息")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id) {
        Role byId = roleService.getById(id);
        List<Role> list = roleService.quanxianList(id);
        return Result.ok(byId);
    }
}
